﻿namespace Guru.Framework.Util
{
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class DataUtil
    {
        #region [ Get Page Of Table ]

        public static DataTable GetPageOfTable(DataTable dt, int page_id, int row_count)
        {
            try
            {
                DataTable new_dt = new DataTable();
                foreach (DataColumn item in dt.Columns)
                {
                    new_dt.Columns.Add(new DataColumn(item.ColumnName, item.DataType));
                }

                int total_row = dt.Rows.Count;

                int rw = page_id * row_count;

                int rowForCount = total_row > (rw + row_count) ? row_count : (total_row - rw);

                for (int i = 0; i < rowForCount; i++)
                {
                    DataRow row = dt.Rows[rw + i];

                    new_dt.Rows.Add(row.ItemArray);
                }

                return new_dt;
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        #endregion


        #region [ Get Column Numbers Of DataTable ]

        /// <summary>
        /// Returns a DataTable with Selected column names.
        /// </summary>
        /// <param name="dt">DataTable object</param>
        /// <param name="columnList"> column names array </param>
        /// <returns>Returns a DataTable with Selected column names.</returns>
        public static DataTable GetColumnsOfDataTable(DataTable dt, params string[] columnList)
        {
            try
            {
                DataTable ndt = new DataTable();
                DataColumn dtCol = null;
                foreach (string colName in columnList)
                {
                    dtCol = new DataColumn(dt.Columns[colName].ColumnName, dt.Columns[colName].DataType);
                    ndt.Columns.Add(dtCol);
                }
                List<object> rowItems = null;
                foreach (DataRow row in dt.Rows)
                {
                    rowItems = new List<object>();
                    foreach (string colName in columnList)
                    {
                        rowItems.Add(row[colName]);
                    }
                    ndt.Rows.Add(rowItems.ToArray());
                }
                return ndt;
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        #endregion


        #region [ Get Columns Of DataTable ColumnNumbers ]

        /// <summary>
        /// Returns a DataTable with Selected column numbers.
        /// </summary>
        /// <param name="dt">DataTable object</param>
        /// <param name="columnList">column numbers array</param>
        /// <returns>Returns a DataTable with Selected column numbers.</returns>
        public static DataTable GetColumnsOfDataTable(DataTable dt, params int[] columnList)
        {
            try
            {
                DataTable ndt = new DataTable();
                DataColumn dtCol = null;
                foreach (int colNo in columnList)
                {
                    dtCol = new DataColumn(dt.Columns[colNo].ColumnName, dt.Columns[colNo].DataType);
                    ndt.Columns.Add(dtCol);
                }
                List<object> rowItems = null;
                foreach (DataRow row in dt.Rows)
                {
                    rowItems = new List<object>();
                    foreach (int colNo in columnList)
                    {
                        rowItems.Add(row[colNo]);
                    }
                    ndt.Rows.Add(rowItems.ToArray());
                }
                return ndt;
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        #endregion


        #region [ Update DataTable With RefColumn And Value]

        public static void UpdateDataTableWithRefColumnAndValue(DataTable dt, string destinationColumn, object destinationValue,
            string refColumn, object refValue)
        {
            try
            {
                foreach (DataRow row in dt.Rows)
                {
                    if (row[refColumn].ToString().Equals(refValue.ToString()))
                    {
                        row[destinationColumn] = destinationValue;
                    }
                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }


        #endregion


        #region [ Get Object With Selected Column ]

        public static object GetObjectWithSelectedColumn(DataTable dt, string refColumn, object refValue, string destinationColumn)
        {
            try
            {
                object retObj = null;
                foreach (DataRow row in dt.Rows)
                {
                    if (row[refColumn].ToString().Equals(refValue.ToString()))
                    {
                        retObj = row[destinationColumn];
                        break;
                    }
                }
                return retObj;
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        #endregion


        #region [ Get One Column Of DataTable as string list ]

        public static List<string> GetOneColumnAsList(DataTable dt, string colName)
        {
            try
            {
                List<string> cols = new List<string>();
                foreach (DataRow dr in dt.Rows)
                {
                    if (cols.Contains(dr[colName].ToString()) == false)
                        cols.Add(dr[colName].ToString());
                }
                return cols;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static List<string> GetOneColumnAsList(DataTable dt, int colIndx)
        {
            try
            {
                List<string> cols = new List<string>();
                foreach (DataRow dr in dt.Rows)
                {
                    cols.Add(dr[colIndx].ToString());
                }
                return cols;
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion


        #region [ FilterDataTableWithOneColumnValueStarts ]

        public static DataTable FilterDataTableWithOneColumnValueStarts(DataTable dt, string filterColumn, object filterValue)
        {
            try
            {
                DataTable newDT = new DataTable();
                foreach (DataColumn col in dt.Columns)
                {
                    newDT.Columns.Add(new DataColumn(col.ColumnName, col.DataType));
                }

                foreach (DataRow row in dt.Rows)
                {
                    if (row[filterColumn].ToString().ToLower().StartsWith(filterValue.ToString().ToLower()) == true)
                    {
                        newDT.Rows.Add(row.ItemArray);
                    }
                }

                return newDT;
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion


        #region [ Filter DataTable with OneColumn ]

        public static DataTable FilterDataTableWithOneColumn(DataTable dt, string filterColumn, object filterValue)
        {
            try
            {
                DataTable newDT = new DataTable();
                foreach (DataColumn col in dt.Columns)
                {
                    newDT.Columns.Add(new DataColumn(col.ColumnName, col.DataType));
                }

                foreach (DataRow row in dt.Rows)
                {
                    if (row[filterColumn].ToString().Equals(filterValue.ToString()) == true)
                    {
                        newDT.Rows.Add(row.ItemArray);
                    }
                }

                return newDT;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static DataTable FilterDataTableWithOneColContains(DataTable dt, string filterColumn, object filterValue)
        {
            try
            {
                DataTable newDT = new DataTable();
                foreach (DataColumn col in dt.Columns)
                {
                    newDT.Columns.Add(new DataColumn(col.ColumnName, col.DataType));
                }

                foreach (DataRow row in dt.Rows)
                {
                    if (row[filterColumn].ToString().ToLower().Contains(filterValue.ToString()) == true)
                    {
                        newDT.Rows.Add(row.ItemArray);
                    }
                }

                return newDT;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static DataTable FilterDataTableWithOneColContains(DataTable dt, int filterIndx, object filterValue)
        {
            try
            {
                DataTable newDT = new DataTable();
                foreach (DataColumn col in dt.Columns)
                {
                    newDT.Columns.Add(new DataColumn(col.ColumnName, col.DataType));
                }

                foreach (DataRow row in dt.Rows)
                {
                    if (row[filterIndx].ToString().Contains(filterValue.ToString()) == true)
                    {
                        newDT.Rows.Add(row);
                    }
                }

                return newDT;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static DataTable FilterDataTableWithOneColumn(DataTable dt, int filterIndx, object filterValue)
        {
            try
            {
                DataTable newDT = new DataTable();
                foreach (DataColumn col in dt.Columns)
                {
                    newDT.Columns.Add(new DataColumn(col.ColumnName, col.DataType));
                }

                foreach (DataRow row in dt.Rows)
                {
                    if (row[filterIndx].ToString().Equals(filterValue.ToString()) == true)
                    {
                        newDT.Rows.Add(row);
                    }
                }

                return newDT;
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion


        #region [ Filter DataTable with Date LaterOrBefore]

        public static DataTable FilterDataTableWithDateLaterOrBefore(DataTable dt, string filterColumn, DateTime searchdate, bool laterOrBefore)
        {
            try
            {
                DataTable newDT = new DataTable();
                foreach (DataColumn col in dt.Columns)
                {
                    newDT.Columns.Add(new DataColumn(col.ColumnName, col.DataType));
                }

                if (laterOrBefore == true)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        if (DateTime.Compare(searchdate, DateTime.Parse(row[filterColumn].ToString())) <= 0)
                        {
                            newDT.Rows.Add(row);
                        }
                    }
                }
                else
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        if (DateTime.Compare(searchdate, DateTime.Parse(row[filterColumn].ToString())) >= 0)
                        {
                            newDT.Rows.Add(row);
                        }
                    }
                }
                return newDT;
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion


        #region [ Filter DataTable with Date Between ]

        public static DataTable FilterDataTableWithDateBetween(DataTable dt, string filterColumn, DateTime dtstart, DateTime dtend)
        {
            try
            {
                DataTable newDT = new DataTable();
                foreach (DataColumn col in dt.Columns)
                {
                    newDT.Columns.Add(new DataColumn(col.ColumnName, col.DataType));
                }

                int cmp1, cmp2;
                foreach (DataRow row in dt.Rows)
                {
                    cmp1 = DateTime.Compare(dtstart, DateTime.Parse(row[filterColumn].ToString()));
                    cmp2 = DateTime.Compare(dtend, DateTime.Parse(row[filterColumn].ToString()));
                    if (cmp1 <= 0 && cmp2 >= 0)
                    {
                        newDT.Rows.Add(row);
                    }
                }

                return newDT;
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion


    }
}
